from selenium import webdriver
from scrapy.http import HtmlResponse
import time
import os


class JavaScriptMiddleware(object):
    CHROMEDRIVER = os.path.join(os.path.abspath('../'), 'chromedriver.exe')
    # 无界面模式
    def ChromeDriverNOBrowser(self):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--log-level=3')
        chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
        chrome_options.add_argument("disable-web-security")
        driverChrome = webdriver.Chrome(executable_path=self.CHROMEDRIVER, chrome_options=chrome_options)
        return driverChrome

    def process_request(self, request, spider):
        # if spider.name == "mid":
        driver = self.ChromeDriverNOBrowser()
        driver.get(request.url)
        time.sleep(3)
        js = "var q=document.documentElement.scrollTop=20000"
        driver.execute_script(js)  # 可执行js，模仿用户操作。此处为将页面拉至最底端。
        time.sleep(2)
        body = driver.page_source
        print("访问"+request.url)
        return HtmlResponse(driver.current_url, body=body, encoding='utf-8', request=request)
        # else:
        #s return None
